package com.aditya.personalexpense.dataaccess;

import java.util.Vector;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.aditya.personalexpense.PersonalExpensesApplication;
import com.aditya.personalexpense.database.DatabaseHelper;
import com.aditya.personalexpense.dataobject.CategoryDO;

public class CategoryDA 
{

	public long insertCategory(CategoryDO categoryDO)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 
			 String query = "SELECT * FROM category WHERE name=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+categoryDO.name});
					 if(cursor!=null && cursor.moveToNext())
					 {
//						 updateGeneralSettings(generalSettingsDO);
					 }
					 else
					 {
						 SQLiteStatement stmt    = database.compileStatement("INSERT INTO category (name, access, colorcode) VALUES (?,?,?)");
						 stmt.bindString(1, categoryDO.name);
						 stmt.bindLong(2, categoryDO.access);
						 stmt.bindLong(3, categoryDO.colorcode);
						 appID = stmt.executeInsert();
						 stmt.close();
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("category", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  updateCategory(CategoryDO categoryDO)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement("update category set name= '"+categoryDO.name+"', access = access+1, colorcode ="+categoryDO.colorcode+" where cid="+categoryDO.cid);
					 
					 appID = stmt.executeUpdateDelete();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "category : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	public CategoryDO getCategoryById(int cid)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 CategoryDO categoryDO = null;
			 String query = "SELECT * FROM category WHERE cid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+cid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 categoryDO = new CategoryDO();
							 categoryDO.cid = cursor.getLong(0);
							 categoryDO.name = cursor.getString(1);
							 categoryDO.access = cursor.getLong(2);
							 categoryDO.colorcode = cursor.getLong(3);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("category", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return categoryDO;
		}
		
	}
	
	
	public String getCategoryNameById(int cid)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 String name = null;
			 String query = "SELECT name FROM category WHERE cid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+cid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 name = cursor.getString(0);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("category", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return name;
		}
		
	}
	
	
	public long getCategoryColorById(int cid)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 long colorcode = 0;
			 String query = "SELECT colorcode FROM category WHERE cid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+cid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 colorcode = cursor.getLong(0);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("category", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return colorcode;
		}
		
	}
	

	public Vector<CategoryDO> getAllCategory()
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 Vector<CategoryDO> vecCategoryDO = new Vector<CategoryDO>();
			 String query = "SELECT * FROM category";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 CategoryDO categoryDO = new CategoryDO();
							 categoryDO.cid = cursor.getLong(0);
							 categoryDO.name = cursor.getString(1);
							 categoryDO.access = cursor.getLong(2);
							 categoryDO.colorcode = cursor.getLong(3);
							 vecCategoryDO.add(categoryDO);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("tblParentalCntrolGeneralSettings", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return vecCategoryDO;
		}
	
		
	}
	

	public void deleteCategoryById(int cid)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "cid=?";
					 database.delete("category", whereClause, new String[]{""+cid});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}
	
	
	public void deleteCategoryByName(String name)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "name=?";
					 database.delete("category", whereClause, new String[]{""+name});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}


}
